package com.func.trigger.job;

import com.func.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import com.func.domain.strategy.repository.IStrategyRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @className UpdateAwardStockJob
 * @description 定期执行的从消息队列中获取需要更新的奖品, 更新数据库中的数据
 * @author: func
 * @date 2025/3/2 15:17
 */

@Slf4j
@Component()
public class UpdateAwardStockJob {
    @Resource
    protected IStrategyRepository repository;

    @Scheduled(cron = "0/5 * * * * ?")
    void exec(){
        try {
            log.info("定时任务, 更新奖品消耗库存 [延时队列获取, 降低数据库的更新频次]");
            StrategyAwardStockKeyVO strategyAwardStockKeyVO = repository.takeQueueValue();
            if (null == strategyAwardStockKeyVO) return;
            log.info("定时任务, 更新奖品消耗库存 strategyId : {}, awardId : {}", strategyAwardStockKeyVO.getStrategyId(), strategyAwardStockKeyVO.getAwardId());
            repository.updateStrategyAwardStock(strategyAwardStockKeyVO.getStrategyId(), strategyAwardStockKeyVO.getAwardId());
        } catch (InterruptedException e) {
            log.error("定时任务 : 更新奖品库存失败", e);
        }
    }

}
